#include "dot_custom_tiling.h"
#include "data_utils.h"
#ifndef ASCENDC_CPU_DEBUG
#include "acl/acl.h"
#include "aclrtlaunch_dot_custom.h"
#include "tiling/platform/platform_ascendc.h"
#else
#include "tikicpulib.h"
extern "C" __global__ __aicore__ void dot_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z, DotCustomTilingData tiling);
#endif
extern void GenerateTilingData(uint8_t *tilingBuf, uint32_t blockDim);

int32_t main(int32_t argc, char *argv[])
{
    constexpr uint32_t BLOCK_DIM = 1;
    constexpr uint32_t DATA_TYPE_SIZE[] = {2, 2, 4, 1, 2, 4};
    uint8_t *tiling = nullptr;
    size_t tilingSize = 17 * sizeof(uint32_t);

#ifdef ASCENDC_CPU_DEBUG
    tiling = (uint8_t *)AscendC::GmAlloc(tilingSize);
    ReadFile("./input/input_tiling.bin", tilingSize, tiling, tilingSize);
#else
    CHECK_ACL(aclInit(nullptr));
    int32_t deviceId = 0;
    CHECK_ACL(aclrtSetDevice(deviceId));
    aclrtStream stream = nullptr;
    CHECK_ACL(aclrtCreateStream(&stream));

    uint8_t *xHost, *yHost, *zHost;
    uint8_t *xDevice, *yDevice, *zDevice;

    CHECK_ACL(aclrtMallocHost((void **)(&tiling), tilingSize));
    ReadFile("./input/input_tiling.bin", tilingSize, tiling, tilingSize);
#endif
    GenerateTilingData(tiling, BLOCK_DIM);
    uint32_t dataTypeSize = DATA_TYPE_SIZE[reinterpret_cast<DotCustomTilingData *>(tiling)->dataType];
    size_t ByteSize = reinterpret_cast<DotCustomTilingData *>(tiling)->totalLength * dataTypeSize;

#ifdef ASCENDC_CPU_DEBUG
    constexpr uint32_t DATA_BLOCK_BYTE = 32;
    uint8_t *x = (uint8_t *)AscendC::GmAlloc((ByteSize + DATA_BLOCK_BYTE - 1) / DATA_BLOCK_BYTE * DATA_BLOCK_BYTE);
    uint8_t *y = (uint8_t *)AscendC::GmAlloc((ByteSize + DATA_BLOCK_BYTE - 1) / DATA_BLOCK_BYTE * DATA_BLOCK_BYTE);
    uint8_t *z = (uint8_t *)AscendC::GmAlloc((ByteSize + DATA_BLOCK_BYTE - 1) / DATA_BLOCK_BYTE * DATA_BLOCK_BYTE);

    ReadFile("./input/input_x.bin", ByteSize, x, ByteSize);
    ReadFile("./input/input_y.bin", ByteSize, y, ByteSize);

    AscendC::SetKernelMode(KernelMode::AIV_MODE);

    ICPU_RUN_KF(dot_custom, BLOCK_DIM, x, y, z,
                *reinterpret_cast<DotCustomTilingData *>(tiling)); // use this macro for cpu debug

    WriteFile("./output/output_z.bin", z, ByteSize);

    AscendC::GmFree((void *)x);
    AscendC::GmFree((void *)y);
    AscendC::GmFree((void *)z);
    AscendC::GmFree((void *)tiling);
#else
    CHECK_ACL(aclrtMallocHost((void **)(&xHost), ByteSize));
    CHECK_ACL(aclrtMallocHost((void **)(&yHost), ByteSize));
    CHECK_ACL(aclrtMallocHost((void **)(&zHost), ByteSize));
    CHECK_ACL(aclrtMalloc((void **)&xDevice, ByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&yDevice, ByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
    CHECK_ACL(aclrtMalloc((void **)&zDevice, ByteSize, ACL_MEM_MALLOC_HUGE_FIRST));


    ReadFile("./input/input_x.bin", ByteSize, xHost, ByteSize);
    ReadFile("./input/input_y.bin", ByteSize, yHost, ByteSize);

    CHECK_ACL(aclrtMemcpy(xDevice, ByteSize, xHost, ByteSize, ACL_MEMCPY_HOST_TO_DEVICE));
    CHECK_ACL(aclrtMemcpy(yDevice, ByteSize, yHost, ByteSize, ACL_MEMCPY_HOST_TO_DEVICE));

    ACLRT_LAUNCH_KERNEL(dot_custom)(BLOCK_DIM, stream, xDevice, yDevice, zDevice, 
                                         reinterpret_cast<DotCustomTilingData *>(tiling));
    CHECK_ACL(aclrtSynchronizeStream(stream));

    CHECK_ACL(aclrtMemcpy(zHost, ByteSize, zDevice, ByteSize, ACL_MEMCPY_DEVICE_TO_HOST));
    WriteFile("./output/output_z.bin", zHost, ByteSize);

    CHECK_ACL(aclrtFree(xDevice));
    CHECK_ACL(aclrtFree(yDevice));
    CHECK_ACL(aclrtFree(zDevice));
    CHECK_ACL(aclrtFreeHost(xHost));
    CHECK_ACL(aclrtFreeHost(yHost));
    CHECK_ACL(aclrtFreeHost(zHost));
    CHECK_ACL(aclrtFreeHost(tiling));

    CHECK_ACL(aclrtDestroyStream(stream));
    CHECK_ACL(aclrtResetDevice(deviceId));
    CHECK_ACL(aclFinalize());
#endif
    return 0;
}
